package com.woniu.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class SpendUtils {

    public static String isWorkDay(Date date){
        // 1.创建一个公历对象
        Calendar calendar = new GregorianCalendar();
        // 2.设置一个日期
        calendar.setTime(date);
        // 3.设置时区为东8区
        calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        // 4.获取日期的星期数(是一个数组，下标从0开始，需要减1)
        int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if(week == 6 || week == 7){
            return "0";
        }
        return "1";
    }

    public static String isBusy(Date date, String timeRangeStart, String timeRangeEnd){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        Date parse = null;
        try {
            parse = sdf.parse(sdf.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long startHour = Integer.valueOf(timeRangeStart.substring(0, timeRangeStart.indexOf(":")));
        long startMinute = Integer.valueOf(timeRangeStart.substring(timeRangeStart.indexOf(":") + 1));
        long endHour = Integer.valueOf(timeRangeEnd.substring(0, timeRangeEnd.indexOf(":")));
        long endMinute = Integer.valueOf(timeRangeEnd.substring(timeRangeEnd.indexOf(":") + 1));
        long timeStart = parse.getTime() + (startHour * 60 * 60 * 1000) + (startMinute * 60 * 1000);
        long timeEnd = parse.getTime() + (endHour * 60 * 60 * 1000) + (endMinute * 60 * 1000);
        if(parse.getTime() >= timeStart && parse.getTime() <= timeEnd){
            return "1";
        }
        return "0";
    }

    public static String getParkingTime(Date startDate,Date endDate){
        /*// 1.定义解析的模板
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startDate = null;
        Date endDate = null;
        try {
            // 2.解析日期格式
            startDate = sdf.parse(start);
            endDate = sdf.parse(end);
        } catch (ParseException e) {
            e.printStackTrace();
        }*/
        // 3.获取开始的时间和离开的时间--单位是毫秒
        long startTime = startDate.getTime();
        long endTime = endDate.getTime();
        // 4.获取两个时间的总相差数
        long millisecondSum = endTime - startTime;
        long secondSum = (endTime - startTime) / 1000;
        long minuteSum = (endTime - startTime) / 1000 / 60;
        long hourSum = (endTime - startTime) / 1000 / 60 / 60;
        long daySum = (endTime - startTime) / 1000 / 60 / 60 / 24;
        if(millisecondSum - secondSum * 1000 >= 500){
            secondSum += 1;
        }
        // 5.使用总相差数转换成相应的单位
        String day = String.valueOf(daySum);
        String hour = String.valueOf(hourSum - daySum * 24);
        String minute = String.valueOf(minuteSum - hourSum * 60);
        String second = String.valueOf(secondSum - minuteSum * 60);
        /*String millisecond = String.valueOf(millisecondSum - secondSum * 1000);*/
        // 6.将字符串进行拼接
        String parkingTime = "";
        if(daySum != 0){
            parkingTime = parkingTime + day + "天";
        }
        if(hourSum != 0 || parkingTime.length() > 0){
            parkingTime = parkingTime + hour + "时";
        }
        if(minuteSum != 0 || parkingTime.length() > 0){
            parkingTime = parkingTime + minute + "分";
        }
        if(secondSum != 0 || parkingTime.length() > 0){
            parkingTime = parkingTime + second + "秒";
        }
        /*if((millisecondSum - secondSum * 1000) != 0){
            parkingTime = parkingTime + millisecond + "毫秒";
        }*/
        return parkingTime;
    }

}
